import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.TreeSet;

@WebServlet("/ListStudentServlet")
public class ListStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        BufferedReader br = new BufferedReader(new FileReader("E:/a.txt"));
        /*比较器,01-02就是小到大排序,02-01就是大到小*/
        TreeSet<webStudent> students = new TreeSet<>(new Comparator<webStudent>() {
            @Override
            public int compare(webStudent o1, webStudent o2) {
                int i1 = o2.getScore() - o1.getScore();
                int i2 =i1==0?o2.getAge()-o1.getAge():i1;
                return i2;
            }
        });
        String lina;
        while ((lina=br.readLine())!=null){
            String[] split = lina.split(",");
            webStudent student = new webStudent(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]));
            students.add(student);
        }
        br.close();

        PrintWriter out = response.getWriter();
        for (webStudent student : students) {
            out.println(student.getName()+","+student.getAge()+","+student.hashCode()+"<br>");

        }
    }
}
